package com.zhouys.relection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

//获取类的信息
public class Test7 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class c1 = Class.forName("com.zhouys.relection.User");
        //获得类的名字
        System.out.println(c1.getName());//获取包名 类名
        System.out.println(c1.getSimpleName()); //获取类名

        //获得类的属性
        Field[] fields = c1.getFields();
        fields = c1.getDeclaredFields();
        for (Field field: fields
             ) {
            System.out.println(field);
        }
        Field name = c1.getDeclaredField("name");
        System.out.println(name);

        //获取类的方法

        System.out.println("+++++++++++++++++");
        Method[] methods = c1.getMethods();//获得本类及其父类的所有方法
        for (Method method: methods
             ) {
            System.out.println("正常的"+method);
        }
        methods = c1.getDeclaredMethods();//获取本类的所有方法
        for (Method method:methods
             ) {
            System.out.println("getDeclaredFields"+method);
        }

        //获得指定的方法
        Method getName = c1.getMethod("getName", null);
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(getName);
        System.out.println(setName);

        //获得指定的构造器
        System.out.println("+++++++++++++++++++++++++++++");
        Constructor[] constructors = c1.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
        Constructor[] declaredConstructors = c1.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println("区分"+declaredConstructor);
        }

    }
}
